package com.sitanyun.merchant.guide.weiht;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.sitanyun.merchant.guide.R;

import java.util.List;
import java.util.Map;

/**
 * Created by jzl
 * on 2021/12/30.
 */
public class StepViews extends View {
    private int bgWidth;
    private int bgHeight;
    private float bgRadius;
    private float proRadius;
    private float startX;
    private float stopX;
    private float bgCenterY;
    private int lineBgWidth;
    private int bgColor;
    private int lineProWidth;
    private int proColor;
    private int textPadding;
    private int maxStep;
    private int textSize;
    private int proStep;
    private Paint bgPaint;
    private Paint proPaint;
    private int interval;
    private List<String> titles;
    private Map<String, Integer> map;
    private float startLine;
    private float stopLine;

    public StepViews(Context context) {
        this(context, null);
    }

    public StepViews(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StepViews(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FlowViewHorizontal);
        bgRadius = ta.getDimension(R.styleable.FlowViewHorizontal_h_bg_radius, 10);
        proRadius = ta.getDimension(R.styleable.FlowViewHorizontal_h_pro_radius, 8);
        lineBgWidth = (int) ta.getDimension(R.styleable.FlowViewHorizontal_h_bg_width, 3f);
        bgColor = ta.getColor(R.styleable.FlowViewHorizontal_h_bg_color, Color.parseColor("#cccccc"));
        lineProWidth = (int) ta.getDimension(R.styleable.FlowViewHorizontal_h_pro_width, 2f);
        proColor = ta.getColor(R.styleable.FlowViewHorizontal_h_pro_color, Color.parseColor("#EF5350"));
        textPadding = (int) ta.getDimension(R.styleable.FlowViewHorizontal_h_text_padding, 20);
        maxStep = ta.getInt(R.styleable.FlowViewHorizontal_h_max_step, 6);
        textSize = (int) ta.getDimension(R.styleable.FlowViewHorizontal_h_textsize, 20);
        proStep = ta.getInt(R.styleable.FlowViewHorizontal_h_pro_step, 1);
        ta.recycle();
        initView();
    }

    private void initView() {
        bgPaint = new Paint();
        bgPaint.setAntiAlias(true);   // 锯齿
        bgPaint.setStyle(Paint.Style.FILL);
        bgPaint.setColor(bgColor);
        bgPaint.setStrokeWidth(lineBgWidth);
        bgPaint.setTextSize(textSize);
        bgPaint.setTextAlign(Paint.Align.CENTER);

        proPaint = new Paint();
        proPaint.setAntiAlias(true);
        proPaint.setStyle(Paint.Style.FILL);
        proPaint.setColor(proColor);
        proPaint.setStrokeWidth(lineProWidth);
        proPaint.setTextSize(textSize);
        proPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            bgWidth = widthSize;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            bgHeight = heightSize;
        }

        interval = (int) ((bgWidth-bgRadius*2-getPaddingRight()-getPaddingLeft())/(maxStep - 1));//紧挨着的两个圆心的距离
        startLine = getPaddingLeft() + bgRadius * 2;//作为线的起点
        stopLine = getPaddingLeft() + interval * (maxStep-1);//作为线的终点
        startX = getPaddingLeft() + bgRadius;//作为圆的起始点
        stopX = stopLine + bgRadius;//作为最后一个圆的圆心   bgWidth - bgRadius - getPaddingRight()
        bgCenterY = getPaddingTop()+bgRadius;//作为线和圆心的纵坐标
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawBg(canvas);
        drawProgress(canvas);
        drawText(canvas);
    }

    /**
     * 绘画文本
     * @param canvas
     */
    private void drawText(Canvas canvas) {
        for (int i = 0; i < maxStep; i++) {
            if (i < proStep) {
                if (null != titles && i < titles.size()) {
                    canvas.drawText(titles.get(i), startX + (i * interval), bgCenterY + textPadding + (bgRadius < proRadius ? proRadius : bgRadius), proPaint);
                }
            } else {
                if (null != titles && i < titles.size()) {
                    String title = titles.get(i);
                    if (null == title) {
                        continue;
                    }
                    canvas.drawText(title, startX + (i * interval), bgCenterY + textPadding + (bgRadius < proRadius ? proRadius : bgRadius), bgPaint);
                }
            }
        }
    }

    /**
     * 绘画底部默认进度条
     * @param canvas
     */
    private void drawBg(Canvas canvas) {
        canvas.drawLine(startLine, bgCenterY, stopLine, bgCenterY, bgPaint);
        for (int i = 0; i < maxStep; i++) {
            canvas.drawCircle(startX + (i * interval), bgCenterY, bgRadius, bgPaint);
        }
    }

    /**
     * 绘画进度
     * @param canvas
     */
    private void drawProgress(Canvas canvas) {
        for (int i = 0; i < proStep; i++) {
            canvas.drawCircle(startX + (i * interval), bgCenterY, proRadius, proPaint);

            canvas.drawLine(startX, bgCenterY, startX + (i * interval), bgCenterY, proPaint);
        }
    }

    /**
     * 进度设置
     *
     * @param progress 已完成到哪部
     * @param maxStep  总步骤
     */
    public void setProgress(int progress, int maxStep) {
        proStep = progress;
        this.maxStep = maxStep;
        invalidate();
    }

    public void setTitles(List<String> titles){
        this.titles = titles;
    }

}
